package org.example.springboot3.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.example.springboot3.entity.Message;
import org.example.springboot3.mapper.MessageMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MessageService {
    @Resource
    private MessageMapper messageMapper;


    public List<Message> selectAll(Message message) {
         return messageMapper.selectAll(message);
    }

    public PageInfo<Message> selectPage
           (Message message, Integer pageNum, Integer pageSize)
    {
        PageHelper.startPage(pageNum, pageSize);
        List<Message> list = messageMapper.selectAll(message);
        return new PageInfo<>(list);
    }

    public void addMessage(Message message) {
        message.setCreateTime(new Date());

        messageMapper.addMessage(message);
    }

    public void deleteById(Integer id) {
        messageMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            this.deleteById(id);
        }
    }

    public void replyMessage(Message message) {
        message.setCreateTime(new Date());
        messageMapper.addMessage(message);
    }


    public void likeMessage(Integer id) {
        Message message = messageMapper.selectById(id);
        if (message != null) {
            Integer likeCount = message.getLikeCount();
            if (likeCount == null) {
                likeCount = 0; // 若 likeCount 为 null，将其初始化为 0
            }
            message.setLikeCount(likeCount + 1);
            messageMapper.updateLikeCount(message);
        }
    }

    public List<Message> selectAllMessagesWithHierarchy() {
        return messageMapper.selectAllMessagesWithHierarchy();
    }

    public Message selectById(Integer id) {
         return messageMapper.selectById(id);
    }
}//
